<template>
  <div class="login-wrapper">
    <div class="login-box">
      <h2 class="login-tit">用户登录</h2>
      <el-form class="login-form" :model="loginForm" ref="loginFormRef" :rules="rules" size="default" status-icon>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" clearable />
        </el-form-item>
        <el-form-item prop="userpwd">
          <el-input type="password" v-model="loginForm.userpwd" prefix-icon="el-icon-lock" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loginForm.loading" style="width: 100%" @click="loginSubmit()">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, imageEmits } from 'element-plus'

const router = useRouter()
const route = useRoute()
const loginFormRef = ref(null)
const loginForm = reactive({
  username: '',
  userpwd: '',
  loading: false
})
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'change' },
    { min: 3, max: 20, message: '用户名为3-20个字符', trigger: 'change' }
  ],
  userpwd: [{ required: true, message: '请输入密码', trigger: 'change' }]
}

const loginSubmit = () => {
  loginFormRef.value.validate((valid) => {
    // 这里一定不要忘记是 loginFormRef.value, ref需要使用value
    if (valid) {
      ElMessage.success('登录成功')
      router.push(route.query.redirect || '/')
      // TODO: 调用接口去登录
    }
  })
}
</script>

<style lang="scss" scoped>
.login-wrapper {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  background: url(../assets/images/login_bg2.jpg) no-repeat;
  background-size: cover;
  overflow: hidden;
}
.login-box {
  width: 400px;
  height: 300px;
  border-radius: 8px;
  box-shadow: 0 0 8px var(--shadow-gray-color);
  background-color: var(--login-bg-color);
  padding: 10px 30px;
}
.login-tit {
  margin: 0;
  height: 48px;
  line-height: 48px;
  text-align: center;
  font-size: 18px;
  margin-bottom: 10px;
}

.login-form .el-form-item {
  margin-bottom: 26px;
}
</style>
